package com.yy.bolg.controller.upload;

import com.yy.bolg.constant.ResultCode;
import com.yy.bolg.util.RespenseJsonTransformationUtil;
import com.yy.bolg.util.TencentCOSUploadFileUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 上传文件控制器
 * @author enmaoFu
 * @date 2024-02-29
 */
@RestController
@RequestMapping(value = "/upload")
public class FileUploadController {

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd/");

    /**
     * 上传文件-业主端
     * 服务器访问路径：http://localhost:8080/bolg/upload/uploadFileToCustomer
     * 本地访问路径：http://localhost:8111/upload/uploadFileToCustomer
     * 参数：{"file":文件}
     */
    @PostMapping("/uploadFileToCustomer")
    @ApiOperation(value = "上传文件-业主端端使用", notes = "参数：{\"file\":文件}")
    @RequestMapping(value = "/uploadFileToCustomer",method = RequestMethod.POST, produces="text/html;charset=UTF-8")
    @ResponseBody
    public String uploadFileToCustomer(@RequestPart("file") MultipartFile file) {
        String result;

        if (null == file) {
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, "文件不能为空", new HashMap());
        }
        String filePath = TencentCOSUploadFileUtil.uploadfile(file,"/customer/");

        Map map = new HashMap();
        map.put("data", filePath);
        result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "上传成功", map);

        return result;
    }

    /**
     * 上传文件-电工端
     * 服务器访问路径：http://localhost:8080/bolg/upload/uploadFileToElectrician
     * 本地访问路径：http://localhost:8111/upload/uploadFileToElectrician
     * 参数：{"file":文件}
     */
    @PostMapping("/uploadFileToElectrician")
    @ApiOperation(value = "上传文件-电工端使用", notes = "参数：{\"file\":文件}")
    @RequestMapping(value = "/uploadFileToElectrician",method = RequestMethod.POST, produces="text/html;charset=UTF-8")
    @ResponseBody
    public String uploadFileToElectrician(@RequestPart("file") MultipartFile file) {
        String result;

        if (null == file) {
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, "文件不能为空", new HashMap());
        }
        String filePath = TencentCOSUploadFileUtil.uploadfile(file,"/electrician/");

        Map map = new HashMap();
        map.put("data", filePath);
        result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "上传成功", map);

        return result;
    }

}


